﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeChatAPIDemo.Model;
using WeChatAPIDemo.Model.CheckQR;
using WeChatAPIDemo.Model.GetCache;
using WeChatAPIDemo.Model.GetQR;

namespace WeChatAPIDemo
{
    public partial class FrmMain : Form
    {
        private string _baseUrl = "http://127.0.0.1:8005".TrimEnd('/');
        private string _uuid;
        private string _userName;
        private string _nickName;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnGetQR_Click(object sender, EventArgs e)
        {
            string apiUrl = $"{_baseUrl}/api/Login/GetQR";
            var device = new DeviceInfo
            {
                DeviceID = "555",
                DeviceName = "55PC",
                DeviceOS = "TestDe55vice",
                //Proxy = new ProxyInfo
                //{
                //    ProxyIp = "yourProxyIp",
                //    ProxyPassword = "yourProxyPassword",
                //    ProxyUser = "yourProxyUser"
                //}
            };
            var jsonContent = JsonConvert.SerializeObject(device); // 序列化对象为JSON字符串  
            byte[] data = Encoding.UTF8.GetBytes(jsonContent);
            string result = HttpHelper.Submit(apiUrl, data);
            GetQRResult getQRResult = JsonConvert.DeserializeObject<GetQRResult>(result);
            pictureBox1.ImageLocation = getQRResult.Data.QrUrl;
            _uuid = getQRResult.Data.Uuid;
        }

        private void btnCheckQR_Click(object sender, EventArgs e)
        {
            string apiUrl = $"{_baseUrl}/api/Login/CheckQR?uuid={_uuid}";
            byte[] data = Encoding.UTF8.GetBytes("");
            string result = HttpHelper.Submit(apiUrl, data);
            CheckQRResult checkQRResult = JsonConvert.DeserializeObject<CheckQRResult>(result);
            MessageBox.Show(checkQRResult.Message);
            _userName = string.Empty;
            _nickName = string.Empty;
            if (checkQRResult != null && checkQRResult.Success == "true")
            {
                _userName = checkQRResult.Data.acctSectResp.userName;
                _nickName = checkQRResult.Data.acctSectResp.nickName;
            }
        }

        private void btnGetCacheInfo_Click(object sender, EventArgs e)
        {
            string apiUrl = $"{_baseUrl}/api/Login/GetCacheInfo?wxid={_uuid}";
            byte[] data = Encoding.UTF8.GetBytes("");
            string result = HttpHelper.Submit(apiUrl, data);
            GetCacheResult checkQRResult = JsonConvert.DeserializeObject<GetCacheResult>(result);
            MessageBox.Show(checkQRResult.Message + "\tDeviceid_str:" + checkQRResult.Data.Deviceid_str);
        }

        private void btnSendTxt_Click(object sender, EventArgs e)
        {
            string apiUrl = $"{_baseUrl}/api/Msg/SendTxt";
            var sendTextInfo = new SendTextInfo
            {
                at = "",
                content = "PC",
                toWxid = _userName,
                type = 1,
                wxid = _uuid
            };
            var jsonContent = JsonConvert.SerializeObject(sendTextInfo); // 序列化对象为JSON字符串  
            byte[] data = Encoding.UTF8.GetBytes(jsonContent);
            string result = HttpHelper.Submit(apiUrl, data);
            GetCacheResult checkQRResult = JsonConvert.DeserializeObject<GetCacheResult>(result);
            MessageBox.Show(checkQRResult.Message);
        }
        public static string ImageToBase64(string imagePath)
        {
            ImageFormat imageFormat = ImageFormat.Bmp;
            string extension = Path.GetExtension(imagePath).ToLower().Trim();
            switch (extension)
            {
                case ".png":
                    imageFormat = ImageFormat.Png; break;
                case ".jpg":
                case ".jpeg":
                    imageFormat = ImageFormat.Jpeg; break;
            }
            byte[] imageBytes;
            using (Image image = Image.FromFile(imagePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    // 保存图片到内存流中  
                    image.Save(ms, imageFormat);
                    imageBytes = ms.ToArray();
                }
            }
            // 将字节数组转换为Base64字符串  
            string base64String = Convert.ToBase64String(imageBytes);
            return "data:image/jpg;base64," + base64String;
        }
        private void btnUploadImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "图片文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png";
            if (dlg.ShowDialog() != DialogResult.OK) return;
            string imgBase64 = ImageToBase64(dlg.FileName);
            string apiUrl = $"{_baseUrl}/api/Msg/UploadImg";

            var uploadImgInfo = new UploadImgInfo
            {
                base64 = imgBase64,
                toWxid = _userName,
                wxid = _uuid
            };
            var jsonContent = JsonConvert.SerializeObject(uploadImgInfo);
            byte[] data = Encoding.UTF8.GetBytes(jsonContent);
            string result = HttpHelper.Submit(apiUrl, data);
            UploadImgResult checkQRResult = JsonConvert.DeserializeObject<UploadImgResult>(result);
            MessageBox.Show(checkQRResult.Message);
        }
    }
}
